package com.trafficradio.trafficradio.operation;

import android.location.Location;
import android.os.Handler;
import com.trafficradio.trafficradio.util.Data;
import com.trafficradio.trafficradio.network.OkHttpClientManager;
import com.trafficradio.trafficradio.util.CONSTANT;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by jwzxg on 2016/7/7.
 */
public class Operation {
    public static OkHttpClientManager manager;
    public Operation(Handler handler){

        manager = new OkHttpClientManager(handler);
    }

    public void login(String username,String password){
        Map<String,String> param = new HashMap<String,String>();
        param.put("username",username);
        param.put("password",password);
        manager.requestWtihPost("login.php",param,CONSTANT.LOGIN);
    }
    public void logout(){
        // TODO: 2016/7/8
        Map<String,String> param = new HashMap<String,String>();
        param.put("username","");
        param.put("password","");
        manager.requestWtihPost("updatefile.php",param,CONSTANT.UPDATEIMAGE);
    }
    public String register(String username,String password,String repreat){
        if(!password.equals(repreat))
            return "两次输入密码不相同";
        if(username.length()>20)
            return "用户名长度超出限制";
        Map<String,String> param = new HashMap<String,String>();
        param.put("username",username);
        param.put("password",password);
        manager.requestWtihPost("register.php",param, CONSTANT.REGISTER);
        return null;
    }
    public void updateFile(File file){
        manager.updateFile(file);

    }
    public void loadText(){
        Map<String,String> param = new HashMap<String,String>();
        param.put("Latitude",""+Data.getLocation().getLatitude());
        param.put("Longitude",""+Data.getLocation().getLongitude());

        manager.requestWtihPost("gettext.php",param,CONSTANT.LOADTEXT);
    }

    public static void updateLocation(Location l){
        Map<String,String> param = new HashMap<String,String>();
        param.put("Latitude",""+l.getLatitude());
        param.put("Longitude",""+l.getLongitude());
        param.put("username",Data.getUsername());
        manager.requestWtihPost("location.php",param,CONSTANT.UPDATELOCATION);

    }
}
